<?php
namespace app\admin\controller;
use app\admin\model\Users;
use app\admin\model\Order;
use think\Controller;
use think\Db;
class Win extends Common
{
	public function index(){
		return $this->fetch();
	}

	public function add(){
		if(request() -> isPost()){
			$data = input('post.');
		if($data['ball1']<6){
		    $data['page'] = '跌'; 
            $data['status'] = 0; 
		}else{
		    $data['page'] = '涨'; 
            $data['status'] = 1; 
		}
		$msg = db('result')->where('qishu',$data['qishu'])->find();
		if(!empty($msg['ball1'])){
            $this->error('该期数已经存在');
		}else{
		    /*$data =db('setresult')->insertGetId($data);*/
		    $data = db('result')->where('qishu',$data['qishu'])->update($data);
		    if($data){
		    	 //赔率的设置
		         $set = db('set')->field('allodds')->select();
		         $allodds = $set[0]['allodds'];
		         /*$id = db('setresult')->where('id',$data)->find();*/
		         $qishu = input('post.qishu');
		         $id = db('result')->where('qishu',$qishu)->find();
		         $time = $id['time'];
		         $data1['qishu'] = $id['qishu'];
                 $data1['updown'] = $id['status'];
                 $datas['qishu'] = $id['qishu'];
                 $datas['updown'] = array('neq',$data1['updown']);
	             $alls = db('order')
	             ->field('oid,uid,money,status')
	             ->where($data1)
	             ->select();   
		        foreach($alls as $key => $value){
		           if($value['status'] == 3){	
		            $win[$key]['oid'] = $value['oid'];
		            $win[$key]['uid'] = $value['uid'];
		            $win[$key]['status'] = 1;
		            $win[$key]['winmoney'] = $value['money']*($allodds);
		            $win[$key]['wintime'] = $time;
		           }else{
		           	 $this->error('订单不存在');
		           } 
		        }     
		         if(!empty($win)){
			            $order = new Order;
			            $msg['zhong'] = $order->saveAll($win);
			            $all = db('order')
			                       ->field('uid,SUM(money) as money')
			                       ->group('uid')
			                       ->where($data1)
			                       ->select(); 
			                   foreach($all as $key => $value){
			                    $user = db('users')->field('uid,money')->where('uid',$value['uid'])->select();
			                         foreach ($user as $k => $v) {
			                               $all[$key]['money']= $v['money']+$allodds*$value['money'];
			                         }
			                    } 
			                    $users = new Users;
			                    $msg['users'] = $users->saveAll($all); 
			        }
			        //用户未中奖状态更改
			        $all1 =  db('order')
			             ->field('oid,uid,money,status')
			             ->where($datas)
			             ->select();      
			        foreach($all1 as $key => $value){
			            if($value['status'] == 3){
			                $wins[$key]['oid'] = $value['oid'];
			                $wins[$key]['uid'] = $value['uid'];
			                $wins[$key]['status'] = 0;
			                $wins[$key]['winmoney'] = 0;
			                $wins[$key]['wintime'] = date('Y-m-d H:i:s');
			           }
			        }
			        if(!empty($wins)){
			            $order = new Order;
			            $msg['wei'] = $order->saveAll($wins); 
			        }
		            if(!$alls && !$all1){
		               $open['open'] = 1;
		               $qishu = input('post.qishu');
		               $qi = db('result')->where('qishu',$qishu)->update($open);
		            }
		            if(isset($msg)){
		               $open['open'] = 1;
		               $qishu = input('post.qishu');
		               $qi = db('result')->where('qishu',$qishu)->update($open);	
		            }
			    	$content = '修改了开奖期数';
		        	admin_log($content);
		        	$this->success('修改成功','admin/win/index','',1); 
			        
		        
		    }else{
		    	$this->error('修改失败');
		    }
		  }
		}else{
            $this->error('请求失败');     
		}   
	}
}